package com.yun.zf.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.zf.common.admin.domain.SysDictType;
import com.yun.zf.common.admin.dto.sys.SysDictTypesDto;
import com.yun.zf.common.admin.model.sys.SysDictTypeAddMo;
import com.yun.zf.common.admin.model.sys.SysDictTypeSearchMo;
import com.yun.zf.common.constant.UserConstants;
import com.yun.zf.common.utils.StringUtils;
import com.yun.zf.system.mapper.SysDictDataMapper;
import com.yun.zf.system.mapper.SysDictTypeMapper;
import com.yun.zf.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 字典 业务层处理
 * 
 * @author ruoyi
 */
@Service
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType>  implements ISysDictTypeService
{
    @Autowired
    private SysDictTypeMapper dictTypeMapper;

    @Autowired
    private SysDictDataMapper dictDataMapper;

    /**
     * 根据条件分页查询字典类型
     * 
     * @param dictType 字典类型信息
     * @return 字典类型集合信息
     */
    @Override
    public IPage<SysDictType> selectDictTypeList(SysDictTypeSearchMo dictType)
    {
        QueryWrapper<SysDictType> sysDictTypeQueryWrapper = new QueryWrapper<>();
        LambdaQueryWrapper<SysDictType> wrapper = sysDictTypeQueryWrapper.lambda().eq(StringUtils.isNotEmpty(dictType.getDictType()), SysDictType::getDictType,dictType.getDictType())
                .eq(StringUtils.isNotEmpty(dictType.getDictName()),SysDictType::getDictName,dictType.getDictName())
                .eq(!("all").equals(dictType.getStatus()),SysDictType::getStatus,dictType.getStatus());
        // 排序不为空，则新增排序
        wrapper.orderBy(StringUtils.isNotNull(dictType.getSort().getUpdateTime()),("ascend").equals(dictType.getSort().getUpdateTime()),SysDictType::getUpdateTime);
        Page<SysDictType> page = new Page<>(dictType.getCurrent(), dictType.getPageSize());
        IPage<SysDictType> dicPage = baseMapper.selectPage(page, wrapper);

        return  dicPage;

    }

    @Override
    public List<SysDictTypesDto> getTypes() {

    return baseMapper.selectDictTypeAll();
    }

    /**
     * 根据字典类型ID查询信息
     * 
     * @param dictId 字典类型ID
     * @return 字典类型
     */
    @Override
    public SysDictType selectDictTypeById(Long dictId)
    {

        return dictTypeMapper.selectDictTypeById(dictId);
    }

    /**
     * 根据字典类型查询信息
     * 
     * @param dictType 字典类型
     * @return 字典类型
     */
    public SysDictType selectDictTypeByType(String dictType)
    {
        return dictTypeMapper.selectDictTypeByType(dictType);
    }

    /**
     * 通过字典ID删除字典信息
     * 
     * @param dictId 字典ID
     * @return 结果
     */
    @Override
    public int deleteDictTypeById(Long dictId)
    {
        return dictTypeMapper.deleteDictTypeById(dictId);
    }

    /**
     * 批量删除字典类型信息
     * 
     * @param dictIds 需要删除的字典ID
     * @return 结果
     */
    public void deleteDictByTypes(String[] types)
    {

         dictTypeMapper.deleteDictByTypes(types);
    }

    /**
     * 新增保存字典类型信息
     * 
     * @param dictType 字典类型信息
     * @return 结果
     */
    @Override
    public int insertDictType(SysDictTypeAddMo dictType)
    {
        SysDictType  sysDictType = new SysDictType();
        sysDictType.setDictType(dictType.getDictType());
        sysDictType.setDictName(dictType.getDictName());
        sysDictType.setRemark(dictType.getRemark());
        sysDictType.setStatus(dictType.getStatus());
        return baseMapper.insert(sysDictType);
    }

    /**
     * 修改保存字典类型信息
     * 
     * @param dictType 字典类型信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateDictType(SysDictType dictType)
    {
      SysDictType sysDictType=  baseMapper.selectDictTypeById(dictType.getDictId());
        // 修改字典类型表数据
        QueryWrapper<SysDictType> dictTypeQueryWrapper = new QueryWrapper<>();
        dictTypeQueryWrapper.lambda().eq(SysDictType::getDictId,dictType.getDictId());
        dictType.setUpdateTime(new Date());
        // 修改字典数据表数据
        if (!sysDictType.getDictType().equals(dictType.getDictType())){
            dictDataMapper.updateDictDataType(sysDictType.getDictType(),dictType.getDictType());
        }
        return    baseMapper.update(dictType,dictTypeQueryWrapper);
    }

    /**
     * 校验字典类型称是否唯一

     * @return 结果
     */
    @Override
    public String checkDictTypeUnique(Long dictTypeId, String dictType )
    {
        Long dictId = StringUtils.isNull(dictTypeId) ? -1L : dictTypeId;
        SysDictType sysDictType = dictTypeMapper.checkDictTypeUnique(dictType);

        if (sysDictType!=null&& sysDictType.getDictId().longValue() != dictId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
